package com.lxw.nonghe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxw.nonghe.entity.CooperativeInstitution;
import com.lxw.nonghe.mapper.CooperativeInstitutionMapper;
import com.lxw.nonghe.service.CooperativeInstitutionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

@Service
public class CooperativeInstitutionServiceImpl extends ServiceImpl<CooperativeInstitutionMapper, CooperativeInstitution> implements CooperativeInstitutionService {

    @Override
    public IPage<CooperativeInstitution> getInstitutionPage(int current, int size, String institutionName, String regionCode) {
        Page<CooperativeInstitution> page = new Page<>(current, size);
        return baseMapper.selectCooperativeInstitutionPage(page, institutionName, regionCode);
    }

    @Override
    public CooperativeInstitution getInstitutionByName(String institutionName) {
        return lambdaQuery()
                .eq(CooperativeInstitution::getInstitutionName, institutionName)
                .one();
    }

    @Override
    public Page<CooperativeInstitution> queryInstitution(CooperativeInstitution queryParam, int current, int size) {
        LambdaQueryWrapper<CooperativeInstitution> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(queryParam.getInstitutionId() != null,
                        CooperativeInstitution::getInstitutionId, queryParam.getInstitutionId())
                .eq(queryParam.getLevel() != null,
                        CooperativeInstitution::getLevel, queryParam.getLevel())
                .eq(queryParam.getStatus() != null,
                        CooperativeInstitution::getStatus, queryParam.getStatus())
                .eq(StringUtils.isNotBlank(queryParam.getInstitutionCode()),
                        CooperativeInstitution::getInstitutionCode, queryParam.getInstitutionCode())
                .eq(StringUtils.isNotBlank(queryParam.getRegionCode()),
                        CooperativeInstitution::getRegionCode, queryParam.getRegionCode());

        wrapper.like(StringUtils.isNotBlank(queryParam.getInstitutionName()),
                        CooperativeInstitution::getInstitutionName, queryParam.getInstitutionName())
                .like(StringUtils.isNotBlank(queryParam.getAddress()),
                        CooperativeInstitution::getAddress, queryParam.getAddress())
                .like(StringUtils.isNotBlank(queryParam.getContactPerson()),
                        CooperativeInstitution::getContactPerson, queryParam.getContactPerson())
                .like(StringUtils.isNotBlank(queryParam.getContactPhone()),
                        CooperativeInstitution::getContactPhone, queryParam.getContactPhone());

        wrapper.orderByDesc(CooperativeInstitution::getCreateTime);

        Page<CooperativeInstitution> page = new Page<>(current, size);
        return page(page, wrapper);
    }
}
